import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  OnInit,
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  PipelineNodeServiceProxy,
  MaterialOutServiceProxy,
  PipelineListDto,
  RequirementMaterialListDto,
  MaterialChargeServiceProxy,
  MaterialChargeListDto,
  MaterialOutListDto,
} from "@shared/service-proxies/service-proxies";
import { LazyLoadEvent, Paginator } from "primeng/primeng";
import { Table } from "primeng/components/table/table";

@Component({
  selector: "yl2",
  templateUrl: "./yl2.component.html",
  styleUrls: ["./yl2.component.less"],
  encapsulation: ViewEncapsulation.None,
})
export class YL2ModalComponent extends AppComponentBase implements OnInit {
  @ViewChild("paginator") paginator: Paginator;
  @ViewChild("dataTable") dataTable: Table;

  bills = new Array<RequirementMaterialListDto>();
  subTaskCode = "";
  drawingCode = "";
  youxianji = 1;
  lastremark = "否";
  advancedFiltersAreShown = false;
  isInit = true; //是否为初始，true，不查询材料
  saving = false;

  filterText = "";

  pipe: PipelineListDto = new PipelineListDto();

  charges = new Array<MaterialChargeListDto>();

  outs = new Array<MaterialOutListDto>();

  //table cols
  cols: any[];
  selectedColumns: any[];
  expandedRowKeys = [];

  constructor(
    injector: Injector,
    private _outMaterialServiceProxy: MaterialOutServiceProxy,
    private _materialChargeServiceProxy: MaterialChargeServiceProxy,
    private _pipelineTaskServiceProxy: PipelineNodeServiceProxy
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.cols = [
      {
        field: "taskCode",
        header: "任务号",
        width: "150px",
        isDefaultSelect: true,
      },
      {
        field: "drawingCode",
        header: "图号",
        width: "120px",
      },
      {
        field: "materialName",
        header: "名称",
        width: "100px",
      },
      {
        field: "invFurBatch",
        header: "炉批号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgz_zjd",
        header: "质检编号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "paiHao",
        header: "牌号",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgzNo",
        header: "合格证",
        width: "120px",
      },
      {
        field: "invCode",
        header: "物质编码",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "materialStatus",
        header: "材料状态",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "materialSpec",
        header: "规格",
        width: "100px",
        isDefaultSelect: true,
      },
      { field: "materialSize", header: "尺寸", width: "100px" },
      { field: "invMeasName", header: "计量单位", width: "100px" },
      {
        field: "techCondition",
        header: "技术状态",
        width: "100px",
        isDefaultSelect: true,
      },
      {
        field: "hgz_zyd",
        header: "质疑单号",
        width: "100px",
      },
      { field: "zhuanYongInfo", header: "专料专用信息", width: "100px" },
    ];

    this.selectedColumns = this.cols.filter((el) => {
      return el.isDefaultSelect;
    });
  }

  load(pipe: PipelineListDto): void {
    this.pipe = pipe;
    this.isInit = true;
    // console.log(pipe);
    // console.log('123');
    this._pipelineTaskServiceProxy
      .getRequirementBillByTask(pipe.id)
      .subscribe((result) => {
        this.bills = result.requirementMaterials;
        this.subTaskCode = result.subTaskCode;
        this.drawingCode = result.drawingCode;
        this.youxianji = result.priority;

        this.getCharges();
        //}
      });

    this._pipelineTaskServiceProxy.getIsShiTai(pipe.id).subscribe(result => {
      this.lastremark = result ? '是' : '否';
    });

    this.getOuts();
  }

  getOuts() {
    this._outMaterialServiceProxy
      .getOutWarehouseBills(this.pipe.id)
      .subscribe((result) => {
        this.outs = result.items;
      });
  }

  getCharges(event?: LazyLoadEvent) {
    if (this.isInit) {
      this.isInit = false;
      return;
    }

    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._materialChargeServiceProxy
      .getMaterialCharges(
        this.filterText,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        undefined,
        5,
        this.primengTableHelper.getSkipCount(this.paginator, event)
      )
      .subscribe((result) => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.charges = result.items;

        this.charges.forEach((el) => {
          this.expandedRowKeys[el.id] = 1;
        });

        this.primengTableHelper.hideLoadingIndicator();
      });
  }

  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());

    this.getOuts();
  }
}
